﻿# encoding=utf-8
# author: walker
# date:
# summary:

import os
import time

import facade
from bs4 import BeautifulSoup
from xjlibrary.our_file_dir import BaseDir

StartTime = time.time()

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -3)
sPath = BaseDir.get_new_path(TopPath, "download", "cnki_bz", "download")
HtmlYearRoot = os.path.join(sPath, 'year_html')

class_dict = {'A': '综合', 'B': '农业、林业', 'C': '医药、卫生、劳动保护', 'D': '矿业', 'E': '石油', 'F': '能源、核技术', 'G': '化工',
              'H': '冶金', 'J': '机械', 'K': '电工', 'L': '电子元器件与信息技术', 'M': '通信、广播', 'N': '仪器、仪表', 'P': '土木、建筑', 'Q': '建材',
              'R': '公路、水路运输', 'S': '铁路', 'T': '车辆', 'U': '船舶', 'V': '航空、航天', 'W': '纺织', 'X': '食品', 'Y': '轻工、文化与生活用品',
              'Z': '环境保护'}
mysqlutils = facade.MysqlUtiles(BaseDir.get_new_path(curPath, "db.ini"), "db", facade.get_streamlogger())



def ClearDb():
    print('-----------清空列表--------------------')
    # conn = MajorDbConnect()
    deleteSql = 'delete from class_year'
    # ExeSqlToDB(deleteSql, conn)
    mysqlutils.ExeSqlToDB(deleteSql)


# 解析年份文件，并入库,输入参数:classes 标准类别代号
#                              class_name 标准类别中文名称
def ProcAllYear(classes, class_name):
    iDict = {}
    YearPath = os.path.join(HtmlYearRoot, classes + '.htm')
    soup = BeautifulSoup(open(YearPath, 'r', encoding='utf-8'), 'lxml')
    alist = soup.find_all('a')
    years = list()
    print('a:')
    for a in alist:
        years.append(a.string)
    print(len(years))

    numlist = soup.find_all('span', style='color:#999;')
    nums = list()
    print('span:')
    for num in numlist:
        nums.append(int(num.string.replace('(', '').replace(')', '')))
    print(len(nums))

    for i in range(len(years)):
        iDict[years[i]] = nums[i]

    # conn = MajorDbConnect()

    for year in sorted(iDict.keys()):
        print('ProcOneYear ' + str(year) + ', ' + str(iDict[year]) + '...')
        sql = "INSERT INTO class_year(classes, years, class_name, num) VALUES('%s', %d, '%s', %d)" % (
            classes, int(year), class_name, iDict[year])
        mysqlutils.ExeSqlToDB(sql)
    #     cur = conn.cursor()
    #     # print(sql)
    #     try:
    #         cur.execute(sql)
    #     except:
    #         print('* errSql:' + sql)
    #         sMsg = '* errSynax:' + traceback.format_exc()
    #     cur.close()
    #     print('Time cost:' + repr(time.time() - StartTime) + '\n')
    # conn.commit()
    # conn.close()


# 用作串行工作
def main(logger=None):
    ClearDb()
    for keys in sorted(class_dict.keys()):
        print(keys + ':' + class_dict[keys])
        ProcAllYear(keys, class_dict[keys])

    print('Game Over!!!')
    print('Time total:' + repr(time.time() - StartTime) + '\n')


if __name__ == '__main__':
    main()
